package day4;

import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author joker
 * @date 2022/4/15 0015 - 12:40
 */
public class Window3 implements Callable {
    private int ticket=100;
    private Lock lock=new ReentrantLock();
    @Override
    public Object call() throws Exception {
        sale();
        return new Object();
    }
    private void sale() {
        while(true){
            try {
                lock.lock();
                if (ticket > 0) {

                    System.out.println(Thread.currentThread().getName() + ":卖票，票号为：" + ticket);
                    ticket--;

                } else {
                    break;
                }
            } finally {
                lock.unlock();
            }
        }
    }

}
